import matrix.*;

/**
 * Created with IntelliJ IDEA.
 * User: aleks_000
 * Date: 29.09.13
 * Time: 17:15
 * To change this template use File | Settings | File Templates.
 */
public class Program {

    private static final long NANOSECONDS_IN_MILLISECOND = 1000000;

    private static final String ELAPSED_TIME_FORMAT = "Elapsed time: %d ms";

    public static void main(String[] args) {
        MatrixMultiplier matrixMultiplier = new SimpleMatrixMultiplier();
        Matrix result = null;


        System.out.println("Empty 4x4 matrix");
        System.out.println("ArrayList matrix");
        Matrix first = ArrayListMatrix.create(4, 4);
        Matrix second = ArrayListMatrix.create(4, 4);
        long elapsedTime = Test(first, second, matrixMultiplier, true);
        System.out.println(String.format(ELAPSED_TIME_FORMAT, elapsedTime));

        System.out.println("LinkedList matrix");
        first = LinkedListMatrix.create(4, 4);
        second = LinkedListMatrix.create(4, 4);
        elapsedTime = Test(first, second, matrixMultiplier, true);
        System.out.println(String.format(ELAPSED_TIME_FORMAT, elapsedTime));
        System.out.println();

        System.out.println("Identity matrices");
        System.out.println("ArrayList matrix");
        first = ArrayListMatrix.createIdentity(4);
        second = ArrayListMatrix.createIdentity(4);
        elapsedTime = Test(first, second, matrixMultiplier, true);
        System.out.println(String.format(ELAPSED_TIME_FORMAT, elapsedTime));

        System.out.println("LinkedList matrix");
        first = LinkedListMatrix.createIdentity(4);
        second = LinkedListMatrix.createIdentity(4);
        elapsedTime = Test(first, second, matrixMultiplier, true);
        System.out.println(String.format(ELAPSED_TIME_FORMAT, elapsedTime));
        ArrayListMatrix.parse(second.toString());
        System.out.println();

        System.out.println("100x100 random matrices");
        System.out.println("ArrayList matrix");
        first = ArrayListMatrix.createRandom(100, 100);
        second = ArrayListMatrix.createRandom(100, 100);
        elapsedTime = Test(first, second, matrixMultiplier, false);
        System.out.println(String.format(ELAPSED_TIME_FORMAT, elapsedTime));

        System.out.println("LinkedList matrix");
        first = LinkedListMatrix.createRandom(100, 100);
        second = LinkedListMatrix.createRandom(100, 100);
        elapsedTime = Test(first, second, matrixMultiplier, false);
        System.out.println(String.format(ELAPSED_TIME_FORMAT, elapsedTime));
    }

    private static long Test(Matrix first, Matrix second, MatrixMultiplier matrixMultiplier, boolean showMatrix) {
        try {
            long startTime = System.nanoTime();
            Matrix result = first.multiply(second, matrixMultiplier);
            if (showMatrix) {
                ShowMatrix(result);
            }
            return (System.nanoTime() - startTime) / NANOSECONDS_IN_MILLISECOND;
        }
        catch (MatrixIncompatibleException ex) {
            System.out.println("Matrix incompatible");
            return 0;
        }
    }



    private static void ShowMatrix(Matrix matrix) {
        for (int row = 0; row < matrix.getRowCount(); ++row) {
            for (int column = 0; column < matrix.getColumnCount(); ++column) {
                System.out.print(matrix.getValue(row, column) + "; ");
            }
            System.out.println();
        }
    }
}
